package fr.lip6.meta.ple.plextraction;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

//import meta.lip6.fr.tests.ExampleClassDiagramCreation;
import artefact.generic.Artefact;
import artefact.generic.ConstructionPrimitive;
import artefact.umlClassDiagram.CreateAssociation;
import artefact.umlClassDiagram.CreateAttribute;
import artefact.umlClassDiagram.CreateClass;
import artefact.umlClassDiagram.CreateGeneralization;
import artefact.umlClassDiagram.CreateOperation;
import artefact.umlClassDiagram.CreateOperationRefinement;
import artefact.umlClassDiagram.CreatePackage;
import artefact.umlClassDiagram.CreateStatement;
import artefact.umlClassDiagram.impl.CreateGeneralizationImpl;
import artefact.umlClassDiagram.impl.CreateStatementImpl;

public class Artefact2File {

	static PrintStream out;

	public static void print(Artefact artefact, String fileName){

		File dotFile =new File(fileName);

		FileOutputStream fout;
		try {
			fout = new FileOutputStream(dotFile);
			out = new PrintStream(fout);

			ConstructionPrimitive first = artefact.getFirst();
			out.println(printConstructionPrimitive(first)+";");
			ConstructionPrimitive next = first.getNext();
			while(next!=null){
				out.println(printConstructionPrimitive(next)+";");
				next = next.getNext();
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static String printConstructionPrimitive(ConstructionPrimitive cp){

		if (cp instanceof CreateClass){
			CreateClass cc = (CreateClass)cp;
			return ("CreateClass("+cc.getName()+cc.getLien()+")");
		}

		if (cp instanceof CreateAssociation){
			CreateAssociation ca = (CreateAssociation)cp;
			return ("CreateAssociation("+ca.getSource()+","+ca.getTarget() +ca.getLien()+")");
		}

		if (cp instanceof CreatePackage){
			CreatePackage ca = (CreatePackage)cp;
			return ("CreatePackage("+ca.getName()+ca.getLien()+")");
		}

		if (cp instanceof CreateAttribute){
			CreateAttribute ca = (CreateAttribute)cp;
			return ("CreateAtribute("+ca.getName()+","+ca.getOwner()+ca.getLien()+ ")");
		}

		if (cp instanceof CreateOperation){
			CreateOperation ca = (CreateOperation)cp;
			return ("CreateOperation("+ca.getName()+","+ca.getOwener()+ca.getLien()+ ")");
		}

		if (cp instanceof CreateOperationRefinement){
			CreateOperationRefinement ca = (CreateOperationRefinement)cp;
			return ("CreateOperationRefinement("+ca.getName()+","+ca.getOwner() +","+ca.getSuper()+ca.getLien()+")");
		}
		
		if(cp instanceof CreateGeneralization) {
			CreateGeneralizationImpl ca = (CreateGeneralizationImpl)cp;
			return ("CreateOperationRefinement("+ca.getSub()+","+ca.getSuper()+ca.getLien()+ ")");
		}
		if(cp instanceof CreateStatement) {
			CreateStatementImpl ca = (CreateStatementImpl)cp;
			return ("CreateStatement("+ca.getText()+","+ca.getOwnerOperation()+","+ca.getLien()+ ")");
		}

		return "CreateUML??";
	}

}
